package com.invest.controller.base;
import com.invest.config.CookieUtils;
import com.invest.config.WebSecurityUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>注释</p>
 *
 * @author yuyizhao
 * @version v0.1 2016/3/8 Exp
 */
@Controller
public class HomeController extends  BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class);

    @RequestMapping(value ="/", method = RequestMethod.GET)
    @ResponseBody
    public String login()
    {
        return "success";
    }

    @RequestMapping(value ="/home", method = RequestMethod.GET)
    public String home(HttpServletRequest request, HttpServletResponse response)
    {
        try {
            if(!StringUtils.equals(CookieUtils.getCookieValueByName(request, "username"),getOperator().getId())){
                CookieUtils.addCookie(response, request, "username", getOperator().getId(), 7);
                CookieUtils.addCookie(response, request, "password", getOperator().getPassword(), 7);
            }
        }catch (Exception ex){
            LOGGER.error("add cookie exception",ex);
        }
        if(isAdmin()){
            return "admin/home";
        }
        return "invest/home";
    }
    @RequestMapping(value ="/login", method = RequestMethod.GET)
    public String login( String error, Model model, HttpServletRequest request)
    {
        model.addAttribute("error", WebSecurityUtils.getLoginError(request));
        return "login";
    }

}
